package com.wt.router;

import org.apache.dubbo.common.URL;
import org.apache.dubbo.rpc.Invocation;
import org.apache.dubbo.rpc.Invoker;
import org.apache.dubbo.rpc.RpcException;
import org.apache.dubbo.rpc.cluster.Router;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Auther: wtt
 * @Date: 2022/1/22 13:15
 * @Description:
 */
public class RestartingInstanceRouter implements Router {

    private final ReadRestartInstance restartInstance;
    private final URL url;

    public RestartingInstanceRouter(URL url) {
        this.url = url;
        this.restartInstance = ReadRestartInstance.create();
    }

    @Override
    public URL getUrl() {
        return url;
    }

    @Override
    public <T> List<Invoker<T>> route(List<Invoker<T>> invokers, URL url, Invocation invocation) throws RpcException {
        return invokers.stream()
                .filter(i -> !restartInstance.hasRestartingInstance(
                        i.getUrl().getParameter("remote_application"),
                        i.getUrl().getParameter("ip"))).collect(Collectors.toList());
    }

    @Override
    public boolean isRuntime() {
        return false;
    }

    @Override
    public boolean isForce() {
        return false;
    }

    @Override
    public int getPriority() {
        return 0;
    }
}
